package com.cupcorn.balapkarung.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.balapkarung.GameDefineText;
import com.cupcorn.balapkarung.LocalData;
import com.cupcorn.lib.Utils;
import com.cupcorn.lib.define.iDefines;
import com.cupcorn.lib.display.DisplaySprite;
import com.cupcorn.lib.display.DisplayText;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameState;
import com.cupcorn.onlinescript.OnlineScript;

public class StateHighscoreOnline extends GameState implements GameDefine
{
	
	public static DisplaySprite displaySprite;
	public static DisplayText displayText;
	
	boolean upinside;
	
	final int animateStep = MAIN_MENU_ANIMATION_STEP;
	int animateCount;
	
	boolean ready, quitingSubmenu;
	
	Entity layerMenu;
	
	int position[][];
	
	public static boolean showSelfScore;
	
	float alpha = 0.02f;
	int alphaStep = 50;
	int alphaCount = 0;
	boolean alphaOut, alphaIn;
	
	public StateHighscoreOnline(GameEngine engine)
	{
		super(engine);
	}
	
	public void load()
	{
		displaySprite = new DisplaySprite(engine, GAME_MENU_HIGHSCORE_ONLINE);
		for(int i = 0; i < GAME_MENU_HIGHSCORE_ONLINE.length; i++)
		{
			displaySprite.newSprite(i, engine.hud, false);
		}

		
		layerMenu = new Entity();
		layerMenu.attachChild(displaySprite.getSprite(ONLINE_BINGKAI));
		displaySprite.getSprite(0).attachChild(displaySprite.getSprite(ONLINE_HEADER));
		
		displaySprite.getSprite(ONLINE_BINGKAI).attachChild(displaySprite.getSprite(ONLINE_NAMESCORE_1));
		displaySprite.getSprite(ONLINE_BINGKAI).attachChild(displaySprite.getSprite(ONLINE_NAMESCORE_2));
		displaySprite.getSprite(ONLINE_BINGKAI).attachChild(displaySprite.getSprite(ONLINE_NAMESCORE_3));
		displaySprite.getSprite(ONLINE_BINGKAI).attachChild(displaySprite.getSprite(ONLINE_NAMESCORE_4));
		displaySprite.getSprite(ONLINE_BINGKAI).attachChild(displaySprite.getSprite(ONLINE_NAMESCORE_5));
		displaySprite.getSprite(ONLINE_BINGKAI).attachChild(displaySprite.getSprite(ONLINE_NAMESCORE_6));
		
		displaySprite.getSprite(ONLINE_NAMESCORE_1).attachChild(displaySprite.getSprite(ONLINE_RANK_1));
		displaySprite.getSprite(ONLINE_NAMESCORE_2).attachChild(displaySprite.getSprite(ONLINE_RANK_2));
		displaySprite.getSprite(ONLINE_NAMESCORE_3).attachChild(displaySprite.getSprite(ONLINE_RANK_3));
		
		
		displayText = new DisplayText(engine, engine.displayFont, GameDefineText.HIGHSCORE_ONLINE);
		
		for(int i = 0; i < GameDefineText.HIGHSCORE_ONLINE.length; i++)
		{
			displayText.newText(i, displaySprite.getSprite(0));
		}
		
		initialPosition();
	}
	
	void initialPosition()
	{
		final float padding = 10 * (GameEngine.CAMERA_WIDTH/800);
		final float padding2 = 40 * (GameEngine.CAMERA_WIDTH/800);
		float x1 = padding;
		float x2 = GameEngine.CAMERA_WIDTH/3 + padding2;
		float ySpace = GameEngine.CAMERA_HEIGHT/9;
		float yIncrement = 50 * (GameEngine.CAMERA_HEIGHT/480);
		for(int i = 0; i < GameDefineText.HIGHSCORE_ONLINE.length/2; i++)
		{
			displaySprite.getSprite(ONLINE_NAMESCORE_1+i).setPosition(x1-padding, yIncrement);
			displayText.text[i*2].setPosition(x1, yIncrement);
			displayText.text[i*2+1].setPosition(x2, yIncrement);
			yIncrement+=ySpace;
		}
		
		displaySprite.getSprite(ONLINE_RANK_1).setX(displaySprite.getSprite(ONLINE_NAMESCORE_1).getWidth() - displaySprite.getSprite(ONLINE_RANK_1).getWidth());
		displaySprite.getSprite(ONLINE_RANK_2).setX(displaySprite.getSprite(ONLINE_NAMESCORE_2).getWidth() - displaySprite.getSprite(ONLINE_RANK_1).getWidth());
		displaySprite.getSprite(ONLINE_RANK_3).setX(displaySprite.getSprite(ONLINE_NAMESCORE_3).getWidth() - displaySprite.getSprite(ONLINE_RANK_1).getWidth());
	}
	
	public void init()
	{
		upinside = false;
		
		animateCount = 0;
		ready = false;
		quitingSubmenu = false;
		
		engine.hud.attachChild(layerMenu);
		layerMenu.setY(- GameEngine.CAMERA_HEIGHT);
		displaySprite.getSprite(ONLINE_YOURRANK).setAlpha(1f);
		alphaIn = false;
		alphaOut = true;
		alphaCount = 0;
	}
	
	public void update(float deltaTime)
	{
		
		if(quitingSubmenu)
		{
			if(animateBackTransition())
			{
				engine.nextState(STATE_HIGHSCORE, iDefines.MESSAGE_STATE_UPDATE);
				removeAllObject();
			}
			
			return;
		}
		
		if(ready)
		{
			upinside = false;
			
			if(alphaOut)
			{
				displaySprite.getSprite(ONLINE_YOURRANK).setAlpha(displaySprite.getSprite(ONLINE_YOURRANK).getAlpha() - alpha);
				alphaCount++;
				
				if(alphaCount == 50)
				{
					alphaOut = false;
					alphaIn = true;
				}
			}
			else if(alphaIn)
			{
				displaySprite.getSprite(ONLINE_YOURRANK).setAlpha(displaySprite.getSprite(ONLINE_YOURRANK).getAlpha() + alpha);
				alphaCount--;
				
				if(alphaCount == 0)
				{
					alphaOut = !false;
					alphaIn = !true;
				}
			}
			
			return;
		}
		
		
		if(animateTransition())
		{
			ready = true;
		}
	}
	
	public void exit()
	{	
		
	}
	
	public boolean keyUp(int keyCode, KeyEvent event)
	{
		if(quitingSubmenu || !ready)
		{
			return false;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
		return false;
	}
	
	public void sceneTouch(Scene scene, TouchEvent touchEvent)
	{	
		if(!ready || quitingSubmenu)
		{
			return;
		}
		if(touchEvent.isActionUp() && !upinside)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if(pSceneTouchEvent.isActionUp())
		{
			if(pTouchArea.equals(displaySprite.getSprite(0)))
			{
				upinside = true;
			}
		}
		return false;
	}
	
	public void click(ClickDetector clickDetector, int pointerID, float sceneX, float sceneY)
	{	
		
	}
	
	public void scrollStarted(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void Scroll(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void scrollFinished(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	private void removeAllObject()
	{
		EngineLock lock = engine.getEngine().getEngineLock();
		lock.lock();
		layerMenu.detachSelf();
		lock.unlock();
	}
	
	boolean animateTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() + (((float)GameEngine.CAMERA_HEIGHT)/animateStep));
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() + StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() + StateMainMenu.farMove);
		
		if(animateCount == animateStep)
		{
			return true;
		}
		
		animateCount++;
		
		return false;
	}
	
	boolean animateBackTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() - StateMainMenu.farMove);
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() - StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() - StateMainMenu.farMove);
		
		if(animateCount == animateStep)
		{
			return true;
		}
		
		animateCount++;
		
		return false;
	}
	
	public static void generateOwnScores()
	{
		displaySprite.getSprite(ONLINE_NAMESCORE_6).setVisible(true);
		displayText.text[GameDefineText.USER_SELF].setText(Utils.getUsername());
		displayText.text[GameDefineText.SCORE_SELF].setText(String.valueOf(LocalData.highscoreOffline));
		setRank(ONLINE_NAMESCORE_6);
	}
	
	public static void ungenerateOwnScores()
	{
		displaySprite.getSprite(ONLINE_NAMESCORE_6).setVisible(false);
		displayText.text[GameDefineText.USER_SELF].setVisible(false);
		displayText.text[GameDefineText.SCORE_SELF].setVisible(false);
	}
	
	public static void setRank(int pos)
	{
		displaySprite.getSprite(pos).attachChild(displaySprite.getSprite(ONLINE_YOURRANK));
		displaySprite.getSprite(ONLINE_YOURRANK).setX(displaySprite.getSprite(pos).getWidth());
	}
}
